<?php
namespace app\admin\controller;
use app\common\business\user\Hd as HdBn;
use think\Db;
class Hd extends Common{
    protected  $dao,$fields,$categorys;
    public function _initialize()
    {
        parent::_initialize();

        $this->dao = db('activity_type');

        unset($fields);
        unset($res);
        $this->assign ('fields',$this->fields);
    }

    public function prize(){

        if(request()->isPost()){

            $model = db('activity_prize');
            $type_id=input('post.type_id');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            if(!empty($type_id) ){
                $map['type_id']=$type_id;
            }
            $list = $model
                ->where($map)
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            $lists = $list['data'];
            foreach ($lists as $k=>$v ){
                switch($v['parent_id']){
                    case 1:
                        $lists[$k]['parent_id'] = '礼品类';
                    break;
                    case 2:
                        $lists[$k]['parent_id'] = '积分类';
                        break;
                    case 3:
                        $lists[$k]['parent_id'] = '再玩一次';
                        break;
                    default:
                        $lists[$k]['parent_id'] = '谢谢参与';
                        break;
                }
                $lists[$k]['is_delete'] = $v['is_delete']?'隐藏':'显示';
            }

            $rsult['data'] = $lists;
            $rsult['count'] = $list['total'];
            $rsult['msg'] = "获取成功";
            $rsult['code'] = 0;
            $rsult['rel'] = 1;
            return $rsult;
        }else{
            return $this->fetch ();
        }
    }
    public function index(){

        if(request()->isPost()){

            $model = db('activity_type');
            $status=input('post.is_delete');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');

            if(!empty($status) ){
                $map['is_delete']=$status;

            }

                $list = $model
                    ->where($map)
                    ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                    ->toArray();

                $lists = $list['data'];
                foreach ($lists as $k=>$v ){

                    $lists[$k]['is_delete'] = $v['is_delete']?'隐藏':'显示';
                }
                $rsult['data'] = $lists;
                $rsult['count'] = $list['total'];


            $rsult['msg'] = "获取成功";
            $rsult['code'] = 0;
            $rsult['rel'] = 1;
            return $rsult;
        }else{
            return $this->fetch ();
        }
    }

    //添加编辑文章
    public function editArticle() {
        $id = request()->param('type_id');
        $model = db('activity_type');
        if (request()->isPost()) {

            $data = input('');
            $result = HdBn::save($data, $id);
            if ($result) {
                return json(['code' => 1, 'msg' => '操作成功!', 'url' => url('index')]);
            } else {
                return json(['code' => 0, 'msg' => '操作失败!']);
            }
        } else {
            $info=$model->where(array('type_id'=>$id))->find();//转入回收站
            $this->assign('info', $info);
            //文章属性
            $this->assign('title', '添加/修改内容');
            return $this->fetch();
        }
    }

    //添加编辑文章
    public function edit() {
        $id = request()->param('prize_id');
        $model = db('activity_prize');
        if (request()->isPost()) {

            $data = input('');
            $result = HdBn::prizesave($data, $id);
            if ($result) {
                return json(['code' => 1, 'msg' => '操作成功!', 'url' => url('prize')]);
            } else {
                return json(['code' => 0, 'msg' => '操作失败!']);
            }
        } else {
            $info=$model->where(array('prize_id'=>$id))->find();//转入回收站
            $this->assign('info', $info);
            //文章属性
            $this->assign('title', '添加/修改内容');
            return $this->fetch();
        }
    }

    public function del(){
        $id = input('post.type_id');
        $model =  db('activity_type');
        $model->where('type_id='.$id)->delete();
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function prizedel(){
        $id = input('post.prize_id');
        $model =  db('activity_prize');
        $model->where('prize_id='.$id)->delete();//转入回收站
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function delAll(){
        $map[] = array('prize_id', 'IN', input('param.ids/a'));
        $model = db('activity_type');
        $model->where($map)->delete();
        $result['code'] = 1;
        $result['msg'] = '删除成功！';
        $result['url'] = url('prize');
        return $result;
    }
    public function showAll(){
        $map[] = array('prize_id', 'IN', input('param.ids/a'));

        $model =db('activity_prize');
        $model->where($map)->update(array('is_delete'=>0));
        $result['code'] = 1;
        $result['msg'] = '显示成功！';
        $result['url'] = url('prize');
        return $result;
    }
    public function hiddenAll(){
        $map[] = array('prize_id', 'IN', input('param.ids/a'));
        $model =db('activity_prize');
        $model->where($map)->update(array('is_delete'=>1));
        $result['code'] = 1;
        $result['msg'] = '隐藏成功！';
        $result['url'] = url('prize');
        return $result;
    }
    public function listorder(){
        $model = $this->dao;
        $catid = input('catid');
        $data = input('post.');
        $model->update($data);
        $result = ['msg' => '排序成功！','url'=>url('index',array('catid'=>$catid)), 'code' => 1];
        return $result;
    }
    public function delImg(){
        if(!input('post.url')){
            return ['code'=>0,'请指定要删除的图片资源'];
        }
        $file = ROOT_PATH.__PUBLIC__.input('post.url');
        if(file_exists($file) && trim(input('post.url'))!=''){
            is_dir($file) ? dir_delete($file) : unlink($file);
        }
        if(input('post.id')){
            $picurl = input('post.picurl');
            $picurlArr = explode(':',$picurl);
            $pics = substr(implode(":::",$picurlArr),0,-3);
            $model = $this->dao;
            $map['id'] =input('post.id');
            $model->where($map)->update(array('pics'=>$pics));
        }
        $result['msg'] = '删除成功!';
        $result['code'] = 1;
        return $result;
    }
    public function getRegion(){
        $Region=db("region");
        $map['pid'] = input("pid");
        $list=$Region->where($map)->select();
        return $list;
    }


}